package org.tea.inventory.admin.web.admin;

import com.github.pagehelper.PageInfo;
import org.tea.inventory.admin.annotation.LoginAdmin;
import org.tea.inventory.base.util.ResponseUtil;
import org.tea.inventory.db.domain.AlumniWorkerWelfare;
import org.tea.inventory.db.service.AlumniWorkerWelfareService;
import org.tea.inventory.db.service.FileSystemStorageService;
import org.tea.inventory.db.util.excel.ExcelParse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/welfare")
public class WorkerWelfareController {

    private static Log log = LogFactory.getLog(WorkerWelfareController.class);
    @Autowired
    private AlumniWorkerWelfareService welfareService;
    @Autowired
    private FileSystemStorageService storageService;
    /**
     * 获取福利信息列表
     */
    @GetMapping("list")
    public Object list(@LoginAdmin Integer adminId,
                       AlumniWorkerWelfare welfare,
                       @RequestParam(value = "page", defaultValue = "1")Integer page,
                       @RequestParam(value = "limit", defaultValue = "10")Integer limit,
                       String sort) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }

        PageInfo<AlumniWorkerWelfare> data = welfareService.querySelective(welfare, page, limit, sort);
        return ResponseUtil.ok(data);
    }

    

    /**
     * 批量导入福利信息
     * @return
     */
    @PostMapping("insertList")
    public Object insertList(@RequestParam("file") MultipartFile welfareImportFile, HttpServletRequest request){
        Map<String, Object> rs = new HashMap<String, Object>();
        if(!welfareImportFile.isEmpty()) {
            rs.put("state", "success");
            rs.put("error", "添加成功");
            String fileName = welfareImportFile.getOriginalFilename();
            if(fileName.endsWith(".xlsx") || fileName.endsWith(".xls")) {

                Map<String, String> storageInfo = storageService.storeFile(welfareImportFile);

                if(null != storageInfo && !storageInfo.isEmpty()){
                    try{
                        List<AlumniWorkerWelfare> listWelfare = ExcelParse.reader(storageInfo.get("absolute_path"));

                        if(listWelfare != null && listWelfare.size() > 0){
                            rs.put("total", listWelfare.size());
                            //开启线程执行
                            Thread childThread = new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    int index = 0;
                                    System.out.println("线程开始执行了！！！");
                                    for(AlumniWorkerWelfare welfare : listWelfare){
                                        String employeeNo = welfare.getEmployeno();
                                        String welfareName = welfare.getWelfareName();
                                        if(employeeNo == null || employeeNo.isEmpty()
                                                || welfareName == null || welfareName.isEmpty()){
                                            log.info("Excel 中第 " + index + " 行的 员工号、福利名称 不能为空.");
                                        }
                                    }
                                    welfareService.insertWelfareBatch(listWelfare);

                                    System.out.println("线程执行完成了！！！");
                                }
                            });
                            childThread.start();
                        }

                    } catch(Exception e){
                        e.printStackTrace();
                    }
                } else {
                    rs.put("state", "fail");
                    rs.put("error", "无效的excel文件!");
                }

            } else {
                rs.put("state", "fail");
                rs.put("error", "无效的excel文件");
            }
        } else {
            rs.put("state", "fail");
            rs.put("error", "文件上传失败");
        }
        return ResponseUtil.ok(rs);
    }

    /**
     * 更新信息
     */
    @PostMapping("update")
    public Object update(@LoginAdmin Integer adminId, @RequestBody AlumniWorkerWelfare welfare) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }
        welfareService.update(welfare);
        return ResponseUtil.ok(welfare);
    }

    /**
     * 删除信息
     */
    @PostMapping("delete")
    public Object delete(@LoginAdmin Integer adminId, @RequestBody AlumniWorkerWelfare welfare) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }

        if(welfare == null) {
            return ResponseUtil.badArgumentValue();
        }

        welfareService.delete(welfare.getId());

        return ResponseUtil.ok();
    }
}
